﻿/*
  Copyright (c) 2011, Mikheev Rostislav
  Copyright (c) 2011, FreshFarsh team

  Steel Engine source code is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
/**
  @author Mikheev Rostislav <hacenator@gmail.com>
  @date 13.03.2011
  @brief File IO class
*/

#ifndef STEEL_IOFILE_H_
#define STEEL_IOFILE_H_

#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
//#include <io.h> // Commented when linux port
// LINUX
//#include <dirent.h>
#include "common.h"
//#include "log.h"

//using namespace std;

namespace steel {

// IOFile
class IOFile {
 private:
	FILE *_file_handler; ///< file handler
	long _file_size; ///< size of file
  char *_file_name; ///< name of file

 public:
	IOFile();
	IOFile(const char *filename, const char *mode);
	~IOFile();

	/**
	 * Open file
   * @param filename
   *	Path to file
   * @param mode
   *	Mode of file opening
   * @return
   *	true if file opened, false otherwise
   */
  bool Open(const char *filename, const char *mode);

  /**
	 * Closing file
   */
  void Close();

	/**
	 * Get file size
   * @return
   * Size of file
   */
	int Size();

  /**
   * Seek to position
   * TODO: Doc
   */
  int Seek(long offset, int whence);

  /**
   * Tell position
   * TODO: Doc
   */
  long Tell();

  /**
	 * Reading from file to buffer
   * @param buffer
   *	Where to reading
   * @param size
   *	Count of bytes to read
   * @return
   *	Count of really readed bytes
   */
  int Read(const void* buffer, const int size = 0);

  /**
	 * Write buffer to file
   * @param buffer
   *	Buffer to write
   * @param size
   *  Count of bytes to write
   * @return
   *  True is write successfull, or False otherwise
   */
  bool Write(const void* buffer, const int size);

  /**
	 * Write one string to file
   * @param buffer
   *	Buffer to write
   */
  void WriteString(const char* buffer);

	/**
	 * File is opened or not
   * @return
   * true if file is opened, false otherwise
   */
  bool Opened() { return _file_handler != NULL; }

  /**
   * Return name of opened file
   */
  const char *filename() { return _file_name; }

	/**
	 *
   */
  FILE *file() {
    return _file_handler;
  }

	};

	//--------------------------------------------------------------------------
	// Utils function
	//--------------------------------------------------------------------------
	/**
	* Checking if selected file exist
	* @ return
	* true if file exists, false otherwise
	*/
	bool FileExists(const char *filename);
}
#endif //
